/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataConverters.DataReaders.readersImpl;

import DataConverters.DataReaders.AbstractDataReader;
import DataConverters.exceptions.DataReaderException;
import DataConverters.interfaces.IConvertable;
import common.gui.progress.IProgressItem;
import DataConverters.interfaces.dataformulaobject.impl.DefaultCellBuilder;
import DataConverters.interfaces.dataformulaobject.intefaces.CellBuilder;
import FileInformation.FileInformation;
import java.io.File;
import java.io.IOException;
import jxl.BooleanCell;
import jxl.BooleanFormulaCell;
import jxl.Cell;
import jxl.CellType;
import jxl.NumberCell;
import jxl.NumberFormulaCell;
import jxl.Sheet;
import jxl.StringFormulaCell;
import jxl.Workbook;
import jxl.biff.formula.FormulaException;
import jxl.read.biff.BiffException;

/**
 *
 * @author Nobody
 */
public class ExcelReader extends AbstractDataReader {

    public ExcelReader(IProgressItem progressItem, IConvertable file) {
        super(progressItem, file);
    }

    @Override
    public void read() throws DataReaderException {
        try {
            FileInformation fileInformation = (FileInformation) source;
            Workbook workbook = Workbook.getWorkbook(fileInformation.getFile());
            Sheet sheet = workbook.getSheet(fileInformation.getChoosedSheetName());
            if (sheet == null) {
                sheet = workbook.getSheet(fileInformation.getExcelSheetNames()[0]);
            }
            readDataFromSheet(sheet);
        } catch (IOException ex) {
            throw new DataReaderException(ex.getMessage(), ex);
        } catch (FormulaException ex) {
            throw new DataReaderException(ex.getMessage(), ex);
        } catch (BiffException ex) {
            throw new DataReaderException(ex.getMessage(), ex);
        }
    }

    private void readDataFromSheet(Sheet sheet) throws FormulaException {
        formulasList.clear();
        progressItem.beforeReading();
        progressItem.setMaximum(sheet.getColumns() * sheet.getRows());
        int progress = 0;
        CellBuilder cellBuilder = new DefaultCellBuilder();
        for (int j = 0; j < sheet.getColumns(); ++j) {
            for (int i = 0; i < sheet.getRows(); ++i) {
                Cell cell = sheet.getCell(j, i);

                DataConverters.interfaces.dataformulaobject.intefaces.Cell cellToCreate = null;
                if (cell.getType() == CellType.NUMBER) {
                    NumberCell numberCell = (NumberCell) cell;
                    cellBuilder.createNumberCell(j, i, numberCell.getValue());
                } else if (cell.getType() == CellType.LABEL) {
                    cellBuilder.createLabelCell(j, i, cell.getContents());
                } else if (cell.getType() == CellType.NUMBER_FORMULA) {
                    NumberFormulaCell formulaCell = ((NumberFormulaCell) cell);
                    cellBuilder.createNumberFormulaCell(j, i, formulaCell.getFormula(), formulaCell.getValue());
                } else if (cell.getType() == CellType.STRING_FORMULA) {
                    StringFormulaCell formulaCell = ((StringFormulaCell) cell);
                    cellBuilder.createStringFormulaCell(j, i, formulaCell.getFormula(), formulaCell.getContents());
                } else if(cell.getType() == CellType.BOOLEAN){
                    BooleanCell booleanCell = (BooleanCell)cell;
                    cellBuilder.createBooleanCell(j, i, booleanCell.getValue());
                } else if(cell.getType() == CellType.BOOLEAN_FORMULA){
                    BooleanFormulaCell booleanCell = (BooleanFormulaCell)cell;
                    cellBuilder.createBooleanFormulaCell(j, i, booleanCell.getFormula(),booleanCell.getValue());
                }

                formulasList.add(cellToCreate);
                progressItem.readed(++progress);
            }
        }
        progressItem.afterReading();
    }

    public static void main(String args[]) throws IOException, DataReaderException, Exception {
        FileInformation file = new FileInformation(new File("E:\\Book1.xls"));
        file.setChoosedSheetName("Sheet1");
        //      new ExcelReader(null).readFile(file);
    }
}
